package com.common.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.common.component.Pagination;
import com.common.manager.MenuInfoManager;
import com.common.manager.Role2MenuManager;
import com.common.manager.SystemInfoManager;
import com.common.model.MenuInfoModel;
import com.common.model.StaffInfoModel;
import com.common.utils.SysConfig;
import com.framework.model.IData;
import com.framework.model.impl.DataMap;
import com.framework.web.BaseController;

@Controller
@RequestMapping(value = "/management/menu")
public class MenuInfoController extends BaseController{

	 private static String modelUrl="/management/menu/";
	 
    @Autowired
	private MenuInfoManager menuInfoManager;
    
    
    @Autowired
	private SystemInfoManager systemInfoManager;
    
    
    @Autowired
   	private Role2MenuManager role2MenuManager;
    
    

    /**
     * 功能：页面分页列表
     * @param vo
     * @param param
     * @return
     */
	@RequestMapping("pageList")
	public ModelAndView pageList(@ModelAttribute("page") Pagination vo,@ModelAttribute("menuInfoModel") MenuInfoModel param) {
		ModelAndView modelView = new ModelAndView(modelUrl+"list");
		List<DataMap> dataList = menuInfoManager.getPageObjectList(vo, param);
		modelView.addObject("vo", vo);
		modelView.addObject("param", param);
		modelView.addObject("objList", dataList);
		this.initMenuList(modelView,new DataMap());
		this.initSystemCode(modelView);
		return modelView;
	}
	

	
	@RequestMapping("menuList")
	public ModelAndView menuList(HttpServletRequest request,HttpServletResponse response){
		
		ModelAndView modelView = new ModelAndView(modelUrl+"list");
		String sysCode = request.getParameter("sysCode");
		try {
			MenuInfoModel param = new MenuInfoModel();
			List<Object> dataList = menuInfoManager.getObjectList(param);
			modelView.addObject("objList", dataList);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return modelView;
	}
	
	
	/**
	 * 功能：初始化添加页面
	 * @param menuInfoModel
	 * @return
	 */
	@RequestMapping("initAdd")
	public ModelAndView initAdd(@ModelAttribute("menuInfoModel") MenuInfoModel menuInfoModel) {
		ModelAndView modelView = new ModelAndView(modelUrl+"add");
		this.initMenuList(modelView,new DataMap());
		this.initSystemCode(modelView);
		log.info("add is page");
		modelView.addObject("vo",menuInfoModel);
		return modelView;
	}
	
	/**
	 * 功能：初始化编辑页面
	 * @param intID
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping("/{intID}/initEdit")
	public ModelAndView initEdit(@PathVariable("intID") String intID) {
		ModelAndView modelView = new ModelAndView(modelUrl+"update");
		IData dataMap = new DataMap();
		dataMap.put("intID", intID);
		MenuInfoModel menuInfoModel = (MenuInfoModel)menuInfoManager.getObjectInfo(dataMap);
		this.initMenuList(modelView,dataMap);
		this.initSystemCode(modelView);
		modelView.addObject("vo",menuInfoModel);
		return modelView;
	}
	
	private void initSystemCode(ModelAndView modelView){
		List systemList = systemInfoManager.getObjectList(new DataMap());
		modelView.addObject("systemList", systemList);
	}
	
	private void initMenuList(ModelAndView modelView,IData dataMap){
		try {
			List<Object> dataList = menuInfoManager.getObjectList("getParentList",dataMap);
			MenuInfoModel menuInfoModel = new MenuInfoModel();
			menuInfoModel.setIntID(0);
			menuInfoModel.setMenuName("顶级菜单");
			menuInfoModel.setParentId(-1);
			dataList.add(0,menuInfoModel);
			modelView.addObject("menuList", dataList);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 功能：执行插入操作
	 * @param menuInfoModel
	 * @return
	 */
	@RequestMapping("add")
	public Object insert(HttpServletResponse response,HttpServletRequest request,@ModelAttribute("menuInfoModel") MenuInfoModel menuInfoModel) {
		try{	
			String message="";
				int opNum = menuInfoManager.insertObjectAndGetID(menuInfoModel);
				if(opNum>0){
					return super.jsonReturn(response,200);
				}else{
					return super.jsonReturn(response,300);
				}
		} catch (Exception e) {
			e.printStackTrace();
			return super.jsonReturn(response,300,e.getMessage());
		}
	}
	
	/**
	 * 功能：执行更新操作
	 * @param menuInfoModel
	 * @return
	 */
	@RequestMapping("update")
	public Object update(HttpServletRequest request,HttpServletResponse response,@ModelAttribute("menuInfoModel") MenuInfoModel menuInfoModel) {
		
		try{
			int opNum =menuInfoManager.updateObject(menuInfoModel);
			if(opNum>0){
					return super.jsonReturn(response,200);
				}else{
					return super.jsonReturn(response,300);
				}
		} catch (Exception e) {
			e.printStackTrace();
			return super.jsonReturn(response,300,e.getMessage());
		}
	}
	
	/**
	 * 功能：执行单记录删除
	 * @param ids
	 * @return
	 */
	@RequestMapping("/{intID}/delete")
	public Object delete(HttpServletResponse response,@PathVariable("intID") int intID) {
		String message="";
		try{
			IData dataMap = new DataMap();
			
			
			dataMap.put("intID", intID);
			int opNum =menuInfoManager.deleteObject(dataMap);
			if(opNum>0){
				return super.jsonReturn(response,200,false);
			}else{
				return super.jsonReturn(response,300,false);
			}
			} catch (Exception e) {
				return super.jsonReturn(response,300,e.getMessage(),false);
			}
	}
	
	/**
	 * 功能：执行批量删除
	 * @param ids
	 * @return
	 */
	@RequestMapping("/deleteAll")
	public Object deleteAll(HttpServletResponse response,HttpServletRequest request) {
		try{
			String ids = request.getParameter("delids");
			IData dataMap = new DataMap();
			dataMap.put("ids", ids);
			String message="";
			int opNum =menuInfoManager.deleteObject("deleteAllObject",dataMap);
			if(opNum>0){
				return super.jsonReturn(response,200,false);
			}else{
				return super.jsonReturn(response,300,false);
			}
		} catch (Exception e) {
			return super.jsonReturn(response,300,e.getMessage(),false);
		}
	}
	
	
	/**
	 * 功能：提供给角色设置使用，角色绑定菜单和对应功能的树
	 * @param response
	 * @param request
	 */
	@ResponseBody
	@RequestMapping("/loadMenuInfo")
	public void loadMenuInfo(HttpServletResponse response,HttpServletRequest request){
		String roleId = request.getParameter("roleId");
		String menuFunc = menuInfoManager.getMenuFuncTreeList(roleId);
		this.ajaxJson(response, menuFunc);
	}

	
	/**
	 * 功能：加载左侧树
	 * @param response
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/loadSubMenuTree")
	public String loadSubMenuTree(HttpServletResponse response,HttpServletRequest request){
		String parentId = request.getParameter("parentId");
		String jsonString="";
		try {
			jsonString = menuInfoManager.loadSystemMenuTree(getSessionUserInfo(),parentId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return super.ajaxJson(response, jsonString);
	}

	
	
	
}